﻿using System.Collections.Generic;
using System.ComponentModel;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using YWM;
using YWM.Localization;

namespace PLCWpfTest.Controls
{
    /// <summary>
    /// Pagination.xaml 的交互逻辑
    /// </summary>
    public partial class Pagination : UserControl
    {
        private readonly int[] _maxResultCounts = new int[] { 20, 50, 100 };

        private readonly Dictionary<string, string> dics = new Dictionary<string, string>
        {
            {"总数","当前第 {0} 页 共 {1} 页" },
            {"每页条数","{0}条/页" },
        };

        private readonly ILocalizationManager _lang = YApp.Resolve<ILocalizationManager>();

        private static readonly DependencyProperty CurrentProperty = DependencyProperty.Register(nameof(Current), typeof(int), typeof(Pagination), new PropertyMetadata(0, CurrentPropertyChangedCallback, null));

        private static readonly DependencyProperty MaxResultCountProperty = DependencyProperty.Register(nameof(MaxResultCount), typeof(int), typeof(Pagination), new PropertyMetadata(10, MaxResultCountPropertyChangedCallback, null));

        private static readonly DependencyProperty TotalProperty = DependencyProperty.Register(nameof(Total), typeof(int), typeof(Pagination), new PropertyMetadata(0, TotalPropertyChangedCallback, null));

        private static readonly DependencyProperty CommandProperty = DependencyProperty.Register(nameof(Command), typeof(ICommand), typeof(Pagination));

        private static readonly DependencyProperty CommandParameterProperty = DependencyProperty.Register(nameof(CommandParameter), typeof(object), typeof(Pagination));

        [Bindable(true), Category("自定义属性"), Description("当前页")]
        public int Current
        {
            get { return (int)GetValue(CurrentProperty); }
            set { SetValue(CurrentProperty, value); }
        }

        [Bindable(true), Category("自定义属性"), Description("每页最大数量")]
        public int MaxResultCount
        {
            get { return (int)GetValue(MaxResultCountProperty); }
            set { SetValue(MaxResultCountProperty, value); }
        }


        [Bindable(true), Category("自定义属性"), Description("总数")]
        public int Total
        {
            get { return (int)GetValue(TotalProperty); }
            set { SetValue(TotalProperty, value); }
        }

        [Bindable(true), Category("自定义属性"), Description("命令")]
        public ICommand Command
        {
            get { return (ICommand)GetValue(CommandProperty); }
            set { SetValue(CommandProperty, value); }
        }

        [Bindable(true), Category("自定义属性"), Description("命令参数")]
        public object CommandParameter
        {
            get { return (ICommand)GetValue(CommandParameterProperty); }
            set { SetValue(CommandParameterProperty, value); }
        }

        public Pagination()
        {
            InitializeComponent();
            foreach (int item in _maxResultCounts)
            {
                SelectCB.Items.Add(string.Format(GetString("每页条数"), item));
            }
            SelectCB.SelectionChanged += ComboBox_SelectionChanged;
        }

        private static void CurrentPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }
            int current = (int)e.NewValue;
            Pagination pagination = (Pagination)d;
            pagination.UpdateControl(current, pagination.MaxResultCount, pagination.Total);
        }

        private static void MaxResultCountPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }
            int maxResultCount = (int)e.NewValue;
            Pagination pagination = (Pagination)d;
            pagination.UpdateControl(pagination.Current, maxResultCount, pagination.Total);
        }

        private static void TotalPropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }
            int total = (int)e.NewValue;
            Pagination pagination = (Pagination)d;
            pagination.UpdateControl(pagination.Current, pagination.MaxResultCount, total);
        }

        private void UpdateControl(int current, int maxResultCount, int total)
        {
            if (total == 0)
            {
                TotalLabel.Content = string.Format(GetString("总数"), 0, 0);
                FirstBtn.IsEnabled = PrevBtn.IsEnabled = LastBtn.IsEnabled = NextBtn.IsEnabled = SelectCB.IsEnabled = false;
                return;
            }
            TotalLabel.Content = string.Format(GetString("总数"), current, total / maxResultCount + (total % maxResultCount == 0 ? 0 : 1));
            FirstBtn.IsEnabled = PrevBtn.IsEnabled = current > 1;
            LastBtn.IsEnabled = NextBtn.IsEnabled = current * maxResultCount < total;
            SelectCB.IsEnabled = true;
            int index = 0;
            for (int i = 0; i < _maxResultCounts.Length; i++)
            {
                if (maxResultCount == _maxResultCounts[i])
                {
                    index = i;
                    break;
                }
            }
            if (index != SelectCB.SelectedIndex)
            {
                SelectCB.SelectionChanged -= ComboBox_SelectionChanged;
                SelectCB.SelectedIndex = index;
                SelectCB.SelectionChanged += ComboBox_SelectionChanged;
            }
        }

        private void FirstBtn_Click(object sender, RoutedEventArgs e)
        {
            Current = 1;
            Execute();
        }

        private void PrevBtn_Click(object sender, RoutedEventArgs e)
        {
            Current--;
            Execute();
        }

        private void NextBtn_Click(object sender, RoutedEventArgs e)
        {
            Current++;
            Execute();
        }

        private void LastBtn_Click(object sender, RoutedEventArgs e)
        {
            Current = Total / MaxResultCount + (Total % MaxResultCount == 0 ? 0 : 1);
            Execute();
        }

        private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            MaxResultCount = _maxResultCounts[((ComboBox)sender).SelectedIndex];
            Current = Total / MaxResultCount + (Total % MaxResultCount == 0 ? 0 : 1);
            Execute();
        }

        private void Execute()
        {
            if (Command != null && Command.CanExecute(CommandParameter))
            {
                Command.Execute(CommandParameter);
            }
        }

        private string GetString(string key)
        {
            return _lang == null ? dics[key] : _lang[key];
        }
    }
}
